package io.jpress.addon.securityinformation.directive;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.JbootControllerContext;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jboot.web.directive.base.PaginateDirectiveBase;
import io.jpress.JPressOptions;
import io.jpress.addon.securityinformation.model.JpressAddonSecurityinformationMfdr;
import io.jpress.addon.securityinformation.service.JpressAddonSecurityinformationMfdrService;
import io.jpress.commons.directive.DirectveKit;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Mfdr 自定义标签
 * anjie 2020年3月26日
 * type 不可空
 * userid 不可空
 */
@JFinalDirective("SecurityInformationMfdrList")
public class SecurityInformationMFDRDirective extends JbootDirectiveBase {

    @Inject
    private JpressAddonSecurityinformationMfdrService service;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {

        Controller controller = JbootControllerContext.get();
        int page = controller.getParaToInt(1, 1);
        Integer pageSize = getParaToInt("pageSize", scope,10);
        String type = getPara("type", scope);
        Long userid = getParaToLong("userid", scope);

        Columns eq = Columns.create().eq("type", type).eq("user_id", userid);
        Page<JpressAddonSecurityinformationMfdr> paginate =
                service.paginateByColumns(page, pageSize, eq);

        scope.setLocal("SecurityInformationMfdrList",paginate);
        renderBody(env, scope, writer);
    }

    private Date strtodata(String str){

        if (str==null || str.trim().equals("")) return null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sdf.parse(str);
        } catch (ParseException e) {
            System.out.println("时间格式不正确");
        }

        return time;
    }

    @JFinalDirective("SecurityInformationMfdrPaginate")
    public static class TemplatePaginateDirective extends PaginateDirectiveBase {

        @Override
        protected String getUrl(int pageNumber, Env env, Scope scope, Writer writer) {
            HttpServletRequest request = JbootControllerContext.get().getRequest();
            String url = request.getRequestURI();
            String contextPath = JFinal.me().getContextPath();

            boolean firstGotoIndex = getPara("firstGotoIndex", scope, false);

            if (pageNumber == 1 && firstGotoIndex) {
                return contextPath + "/";
            }

            // 如果当前页面是首页的话
            // 需要改变url的值，因为 上一页或下一页是通过当前的url解析出来的
            if (url.equals(contextPath + "/")) {
                url = contextPath + "/securityinformationmfdr/index"
                        + JPressOptions.getAppUrlSuffix();
            }
            return DirectveKit.replacePageNumber(url, pageNumber);
        }

        @Override
        protected Page<?> getPage(Env env, Scope scope, Writer writer) {
            return (Page<?>) scope.get("SecurityInformationMfdrList");
        }

    }


    @Override
    public boolean hasEnd() {
        return true;
    }
}
